<%@page import="org.jcms.service.BookServiceI"%>
<%@page import="org.jcms.model.Book"%>
<%@page import="org.jcms.model.OrderBook"%>
<%@page import="org.jcms.service.OrderBookServiceI"%>
<%@page import="org.jcms.uitils.DateUtil"%>
<%@page import="org.jcms.service.ProofServiceI"%>
<%@page import="org.jcms.model.Proof"%>
<%@page import="org.hibernate.Hibernate"%>
<%@page import="org.jcms.model.Customer"%>
<%@page import="org.jcms.model.Order"%>
<%@page
	import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%@page import="org.jcms.service.OrderServiceI"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%
	String orderId = request.getParameter("orderId");
	if (orderId == null) {

		out.print("参数错误");
		return;

	}
	ApplicationContext ac = WebApplicationContextUtils
			.getRequiredWebApplicationContext(this.getServletContext());
	OrderServiceI orderServiceI = (OrderServiceI) ac
			.getBean("orderServiceImpl");

	ProofServiceI proofServiceI = (ProofServiceI) ac
			.getBean("proofServiceImpl");

	OrderBookServiceI orderBookServiceI = (OrderBookServiceI) ac
			.getBean("orderBookServiceImpl");

	BookServiceI bookServiceI = (BookServiceI) ac
			.getBean("bookServiceImpl");
	Order order = orderServiceI.findById(orderId);

	if (order == null) {

		out.print("此订单不存在");
		return;

	}
	Customer customer = (Customer) session.getAttribute("customer");
	if (customer == null
			|| !customer.getCustomerId().equals(
					order.getCustomer().getCustomerId())) {
		out.print("此订单不属于您，您没有查询权限");
		return;
	}

	List<Proof> proofls = proofServiceI.findByHQL(
			"from Proof where order.orderId=?", orderId);
	Proof proof = null;

	if (!proofls.isEmpty())
		proof = proofls.get(0);

	List<OrderBook> orderBookls = orderBookServiceI.findByHQL(
			"from OrderBook as t1 where t1.id.order.orderId = ?",
			orderId);
			boolean isOK = true;
%>

<style>
#ajaxTable tr td {
	border: 1px solid gray;
	font-size: 13px;
}

#ajaxTable tr td a {
	color: blue;
	text-decoration: underline;
}
</style>
<div>
	<table style="border: 1px solid gray;text-align: center;"
		id="ajaxTable">
		<tr>
			<td>订单号:</td>
			<td><div
					style="width: 300px; word-break: break-all; overflow: auto;"><%=order.getOrderId()%></div>
			</td>
		</tr>
		<tr>
			<td>订单所有者ID:</td>
			<td><div
					style="width: 300px; word-break: break-all; overflow: auto;"><%=order.getCustomer().getCustomerId()%></div>
			</td>
		</tr>
		<tr>
			<td>订单所有者名字:</td>
			<td><div
					style="width: 300px; word-break: break-all; overflow: auto;"><%=order.getCustomer().getCustomerName()%></div>
			</td>
		</tr>

		<tr>
			<td>下单时间:</td>
			<td><div
					style="width: 300px; word-break: break-all; overflow: auto;"><%=DateUtil.formatDatetime(order.getOrderTime())%></div>
			</td>
		</tr>

		<tr>
			<td>状态:</td>
			<td><div
					style="width: 300px; word-break: break-all; overflow: auto;"><%=order.getState().getStateName()%></div>
			</td>
		</tr>

		<tr>
			<td>领书单ID:</td>
			<td><div
					style="width: 300px; word-break: break-all; overflow: auto;"><%=proof == null ? "领书单还未生成" : proof.getProofId()%></div>
			</td>
		</tr>

		<tr>
			<td>领书单状态:</td>
			<td><div
					style="width: 300px; word-break: break-all; overflow: auto;"><%=proof == null ? "领书单还未生成" : proof.getProofstate()
					.getProofStateName()%></div>
			</td>
		</tr>
		<tr>
			<td>订单内容:</td>
			<td><div
					style="width: 300px; word-break: break-all; overflow: auto;text-align: left;">
					书目：
					<hr />
					<%
						for (int i = 0; i < orderBookls.size(); ++i) {
					%>
					<div>
						<%
							Book bookTemp = bookServiceI.findById(orderBookls.get(i)
										.getId().getBook().getBookIsbn());
								out.print("书名：" + bookTemp.getBookName() + "<br />金额："
										+ orderBookls.get(0).getOrderBookNumber() + " x "
										+ orderBookls.get(0).getOrderBookPrice() + " = "
										+ orderBookls.get(0).getOrderBookPrice()
										* orderBookls.get(0).getOrderBookNumber());
								out.print("<br />库存：" + bookTemp.getBookStock());

								if (bookTemp.getBookStock() < orderBookls.get(0)
										.getOrderBookNumber()) {
									isOK = false;
									out.print("<span style='color:red;font-weight: bolder;'>库存不足</span>");
								} else
									out.print("<span style='color:blue;font-weight: bolder;'>库存充足</span>");
						%>
					</div>
					<%=i == orderBookls.size() - 1 ? "" : "<hr />"%>

					<%
						}
					%>

				</div>
			</td>
		</tr>



		<tr>
			<td>总金额:</td>
			<td><div
					style="width: 300px; word-break: break-all; overflow: auto;color: red;font-weight: bolder;">

					<div><%=order.getOrderSum()%></div>

				</div>
			</td>
		</tr>

		<%
			if (order.getState().getStateId() == 2) {
		%>
		<tr>

			<td>订单领书状态:</td>
			<td><div
					style="width: 300px; word-break: break-all; overflow: auto;color: red;font-weight: bolder;">



					<div><%=isOK ? "可领" : "暂不可领"%></div>

				</div>
			</td>
		</tr>
		<%
			}
		%>
	</table>

</div>